package lab03.test14;

import java.sql.*;
//14、	利用 JDBC 实现学生表基本信息的增删改查操作。
public class Test14 {
    public static void main(String[] args) {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
            /*7101 宋晓梅  123456  xiaomei@yahoo.com.cn
            7102 关松杰  654321  songjie@263.net
            7103 李晓梅  123654  xiaomei@yahoo.com.cn
            7104 张松岩  456123  songyan@yahoo.com.cn
            7105 王亚杰  321456  yajie@hotmail.com
            7106 曲小声  654321  xiaosheng@263.net
             */
        String insert ="insert INTO "
                + "userinfo(usercode,username,password,email)"
                +"values ('7106','曲小声','654321','xiaosheng@263.net')";
        String select = "select * from userinfo";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",//数据库链接字符串
                    "root",
                    "123456"
            );//建立数据库连接
            statement = connection.createStatement();
//            statement.execute(insert);
            resultSet = statement.executeQuery(select);//执行查询返回结果集
            System.out.println("原来的数据是：");
            while (resultSet.next()){//判断结果集下一个是否还有，没有就不打印
                System.out.print(resultSet.getString("usercode")+"  ");
                System.out.print(resultSet.getString("username")+"  ");
                System.out.print(resultSet.getString("password")+"  ");
                System.out.println(resultSet.getString("email")+"   ");
            }

            statement.execute(insert);
            resultSet = statement.executeQuery(select);//执行查询返回结果集
            System.out.println("现在的数据是：");
            while (resultSet.next()){//判断结果集下一个是否还有，没有就不打印
                System.out.print(resultSet.getString("usercode")+"  ");
                System.out.print(resultSet.getString("username")+"  ");
                System.out.print(resultSet.getString("password")+"  ");
                System.out.println(resultSet.getString("email")+"   ");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

